package com.tiamo.traders.responseadvice;

import com.alibaba.fastjson.JSONObject;
import com.tiamo.traders.entity.ImgModel;
import com.tiamo.traders.entity.ResultModel;
import com.tiamo.traders.myexception.MyException;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.HashMap;
import java.util.Map;


/**
 * ResponseBodyAdvice:相应数据的增强处理，对相应数据做统一处理。
 * 前提：只处理代@ResponseBody的接口，否则不予处理。
 *
 * @ControllerAdvice：控制器增强处理的注解。 ResponseBodyAdvice增强处理逻辑位于控制器之后执行，能够拦截控制器的返回值
 */

@ControllerAdvice
public class MyResponseAdvice implements ResponseBodyAdvice {
    /**
     * suports函数的返回值：true--->正常执行拦截处理，执行beforeBodyWrite（）函数，否则不执行beforeBodyWrite函数
     * MethodParameter:控制器参数
     */

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }


    /**
     * beforeBodyWrite的返回值：最终向客户端相应的数据
     * Object o:控制器的返回值
     */
    @ResponseBody
    @Override
    public Object beforeBodyWrite(Object result, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {

        /**拦截接口中的异常*/

        if (result instanceof MyException) {
            MyException exception = (MyException) result;
            return ResultModel.getResultModel(exception.getCode(), exception.getMsg());
        }

        /** 如果控制器相应的是字符串，需要把响应的字符串手动转换成JSON对象
         * */

        if (result instanceof String && ((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getServletPath().endsWith("/code")) {
            return JSONObject.toJSONString(ResultModel.getResultModel((String) result));
        }

/**对登录做校验，*/

        if (result instanceof String && ((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getServletPath().endsWith("/login")) {
            if (((String) result).equals("success")) {
                return JSONObject.toJSONString(ResultModel.getResultModel((String) result));
            } else {
                return JSONObject.toJSONString(ResultModel.getResultModel((String) result));
            }
        }
        if (result instanceof String && ((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getServletPath().endsWith("/file-upload")) {
            ImgModel imgModel=new ImgModel();
            imgModel.setSrc((String) result);
            //return ResultModel.getResultModel(0,(String) result,imgModel);
            return JSONObject.toJSONString(ResultModel.getResultModel(0,"success",imgModel));
        }
        if (result instanceof HashMap && ((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getServletPath().endsWith("/home")) {
            return ResultModel.getResultModel(0,"success",result);
        }

        if (result instanceof Integer) {
            return ResultModel.getResultModel((Integer) result);
        }

        if (result instanceof String) {
            return JSONObject.toJSONString(ResultModel.getResultModel((String) result));
        }

        if (result instanceof HashMap){
            HashMap res=(HashMap)result;
            System.out.println(res.get("data"));
            return ResultModel.getResultModel(0,"success",(Integer) res.get("count"),res.get("data"));
        }

        return ResultModel.getResultModel(0,"success",result);
    }
}
